<template>
  <div>
    <el-input v-model="rules" readonly>
      <el-button slot="append" icon="el-icon-search" @click="onClick"></el-button>
    </el-input>
    <el-dialog
      title="参数绑定"
      :append-to-body="true"
      :visible.sync="dialogVisible"
      width="30%">

      <duv class="app-dialog">
        <div class="app-tree">
          <el-tree :data="treeData" @node-click="handleNodeClick"></el-tree>
        </div>
        <div class="app-body">
          <el-input
            type="textarea"
            :rows="4"
            v-model="context"
          >
          </el-input>
        </div>
      </duv>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="onSubmit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import {getFormNodeList} from "../../api";

  export default {
    name: 'FormDialog',
    props: {
      id: {
        type: String
      }
    },
    data() {
      return {
        rules: '',
        context: '',
        dialogVisible: false,
        treeData: []
      }
    },
    methods: {
      handlerQuery() {
        if (this.$attrs.value) {
          this.context = this.$attrs.value;
          this.rules = this.$attrs.value;
        }
        getFormNodeList(this.id).then(res => {
          if (res && res.data.code === 20000) {
            this.treeData = res.data.data;
          }
        });
      },
      handleNodeClick(node) {
        if (node) {
          this.context = this.context + node.name;
        }
      },
      onClick() {
        this.dialogVisible = true;
      },
      onSubmit() {
        this.rules = this.context;
        this.$emit('input', this.rules);
        this.dialogVisible = false;
      }
    },
    mounted() {
      this.handlerQuery();
    }
  }
</script>

<style scoped>

  .app-dialog {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: row;
  }

  .app-tree {
    width: 20%;
    height: 100%;
  }

  .app-body {
    width: 100%;
    height: 100%;
  }
</style>
